package org.yixiu.producer;

import org.yixiu.producer.invoke.RequestHandler;
import org.yixiu.producer.invoke.ServiceMap;
import org.yixiu.producer.service.HelloService;
import org.yixiu.producer.service.UpperCaseService;
import org.yixiu.producer.service.impl.HelloServiceImpl;
import org.yixiu.producer.service.impl.UpperCaseServiceImpl;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 服务端启动器
 */
public class RpcProviderMain {
    private static final ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] args) throws IOException {
        HelloService helloService = new HelloServiceImpl();
        UpperCaseService upperCaseService = new UpperCaseServiceImpl();

        ServiceMap.registerService(HelloService.class.getName(),helloService);
        ServiceMap.registerService(UpperCaseService.class.getName(),upperCaseService);

        ServerSocket serverSocket = new ServerSocket(8080);

        while (true){
            final Socket socket = serverSocket.accept();
            executorService.execute(new RequestHandler(socket));
        }
    }
}
